package abs.controller;

import abs.factory.ResultFactory;
import abs.service.UserService;
import abs.vo.Result;
import abs.vo.UserInfo;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.websocket.server.PathParam;
@RestController
public class UserInfoCrl {
    @Autowired
    private UserService userService;

    @RequestMapping(value = "/api/login", method = RequestMethod.POST, produces = "application/json; charset = UTF-8")
    @ResponseBody
    public Result login(@PathParam("username")String username, @PathParam("password") String password){
        UserInfo user = userService.login(username, password);
        if (null == user){
            return ResultFactory.buildFailResult("账户密码错误");
        }
        System.out.println(username+"登陆了");
        return ResultFactory.buildSuccessResult(user);
    }

    @RequestMapping(value = "/api/register", method = RequestMethod.POST, produces = "application/json; charset = UTF-8")
    @ResponseBody
    public Result login(@PathParam("username")String username, @PathParam("nickname")String nickname, @PathParam("password")String password){
        if(null != userService.findUserByName(username)){
            return ResultFactory.buildFailResult("用户名已存在");
        }

        UserInfo user = new UserInfo();
        user.setUserName(username);
        user.setNickName(nickname);
        user.setPassword(password);
        userService.register(user);
        System.out.println(username+"注册了");
        return ResultFactory.buildSuccessResult(user);
    }

    @RequestMapping(value = "/api/getUserById", method = RequestMethod.GET, produces = "application/json; charset = UTF-8")
    @ResponseBody
    public Result getUserById(@PathParam("userName")String userId){
        UserInfo userInfo = userService.findUserByName(userId);
        return ResultFactory.buildSuccessResult(userInfo);
    }

}
